package com.db.gffxo.epi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import com.db.gffxo.epi.domain.Event;
import com.db.gffxo.epi.domain.EventDetail;
import com.db.gffxo.epi.domain.EventPublish;
import com.db.gffxo.epi.format.Formatter;
import com.db.gffxo.epi.repository.EventDao;
import com.db.gffxo.epi.repository.EventPublishDao;
import com.db.gffxo.epi.repository.EventDetailDao;
import com.db.gffxo.epi.publish.Publisher;

public class PublishControllerServicesImpl implements PublishControllerServices {
	
	private Logger log = Logger.getLogger(PublishControllerServicesImpl.class);

	EventDao eventDao = null;
	EventPublishDao eventPublishDao = null;
	EventDetailDao eventDetailDao = null;
	String ourTypes = new String();
		
	HashMap<String,ArrayList<Publisher>> publishers = new HashMap<String,ArrayList<Publisher>>();
		

	public List<EventDetail> getEventDetailBatch() {
		
		return eventDetailDao.getEventDetailbyStatusType(Event.STATUS_Formatted, ourTypes, 10);	
	}
	
	public ArrayList<Publisher> getPublisher (String eventType) {
		return publishers.get(eventType);
		
	}
	
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
	public void updatePublishedEvent (EventDetail eventDetail, boolean success) {
		Event event = eventDetail.getEvent();
		if (success) {
			eventDao.updateStatus(event);
			if (event.getStatus().equals(Event.STATUS_Sent) || event.getStatus().equals(Event.STATUS_NoPublisher)) {
				eventDetailDao.complete(eventDetail);
			}
		} else {
			event.setStatus(Event.STATUS_FailSend);
			eventDao.updateStatus(event);						
		}
	}
	
	
	/* (non-Javadoc)
	 * @see com.db.gffxo.epi.controller.FormatControllerServices#loadFormatters()
	 */
	public void loadPublishers(ApplicationContext applicationContext) {
		
		List<EventPublish> allTypes = eventPublishDao.getEventPublish();
		Iterator<EventPublish> it = allTypes.iterator();
		StringBuffer theseTypes = new StringBuffer("\'");
		String lastType = "";
		ArrayList<Publisher> these = new ArrayList<Publisher>();

		while (it.hasNext()) {
			EventPublish ep = it.next();
			//Get publisher from application context
			try {
				Publisher pub = (Publisher) applicationContext.getBean(ep.getPublisherName());
				pub.configurePublisher();
				if (ep.getFormatterId()!=null)
					pub.addFormat(ep.getFormatterId());
				
				if (!lastType.equals(ep.getEventType())&&!lastType.equals("")) {
					publishers.put(lastType, these);
					these = new ArrayList<Publisher>();
					theseTypes.append(lastType);
					theseTypes.append("\',\'");
				}
				these.add(pub);					
				lastType = ep.getEventType();

			} catch (NoSuchBeanDefinitionException e) {
				e.printStackTrace();
				//TODO
			}
		}
		publishers.put(lastType, these);
		theseTypes.append(lastType);
		theseTypes.append("\',\'");

		setOurTypes(theseTypes.substring(0,theseTypes.length()-2));
	}

	//getEventDao();
	/* (non-Javadoc)
	 * @see com.db.gffxo.epi.controller.FormatControllerServices#getEventDao()
	 */
	public EventDao getEventDao() {
		return eventDao;
	}

	//setEventDao(rs.getEventDao(10));
	/* (non-Javadoc)
	 * @see com.db.gffxo.epi.controller.FormatControllerServices#setEventDao(com.db.gffxo.epi.repository.EventDao)
	 */
	public void setEventDao(EventDao eventDao) {
		this.eventDao = eventDao;
	}

	//getEventFormatDao();
	/* (non-Javadoc)
	 * @see com.db.gffxo.epi.controller.FormatControllerServices#getEventFormatDao()
	 */
	public EventPublishDao getEventPublishDao() {
		return eventPublishDao;
	}

	//setEventFormatDao(rs.getEventFormatDao(10));
	/* (non-Javadoc)
	 * @see com.db.gffxo.epi.controller.FormatControllerServices#setEventFormatDao(com.db.gffxo.epi.repository.EventFormatDao)
	 */
	public void setEventPublishDao(EventPublishDao eventFormatDao) {
		this.eventPublishDao = eventFormatDao;
	}

	//getEventDetailDao();
	/* (non-Javadoc)
	 * @see com.db.gffxo.epi.controller.FormatControllerServices#getEventDetailDao()
	 */
	public EventDetailDao getEventDetailDao() {
		return eventDetailDao;
	}

	//setEventDetailDao(rs.getEventDetailDao(10));
	/* (non-Javadoc)
	 * @see com.db.gffxo.epi.controller.FormatControllerServices#setEventDetailDao(com.db.gffxo.epi.repository.EventDetailDao)
	 */
	public void setEventDetailDao(EventDetailDao eventDetailDao) {
		this.eventDetailDao = eventDetailDao;
	}


	public String getOurTypes() {
		return ourTypes;
	}


	public void setOurTypes(String ourTypes) {
		this.ourTypes = ourTypes;
	}


}
